package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.dao.PositionCollectionDao;
import com.coocaa.internship.dao.PositionListDao;
import com.coocaa.internship.dao.UserLoginDao;
import com.coocaa.internship.pojo.PositionCollection;
import com.coocaa.internship.pojo.PositionList;
import com.coocaa.internship.pojo.UserLogin;
import com.coocaa.internship.service.PositionCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :hhd
 * @Description:
 * @date :2019/8/8 20:47
 */

@Service
public class PositionCollectionServiceImpl implements PositionCollectionService {

    @Autowired
    PositionCollectionDao positionCollectionDao;

    @Autowired
    PositionListDao positionListDao;

    @Autowired
    UserLoginDao userLoginDao;

    @Override
    public ResultData insertPositionCollection(PositionCollection positionCollection) {
        Long positionId_inSql = positionCollectionDao.selectPositionCollectionById(positionCollection);
        if (positionId_inSql != null && positionId_inSql.equals(positionCollection.getPositionId())) {
            return ResultData.success("已经收藏，无需重复收藏");
        } else {
            Integer i = positionCollectionDao.insertPositionCollection(positionCollection);
            if (i > 0) {
                return ResultData.success("收藏成功");
            }
            return ResultData.failed();
        }
    }

    @Override
    public ResultData selectPositionCollectionByUserId(Long userId) {
        UserLogin user = userLoginDao.selectUserLoginById(userId);
        if (user == null) {
            return ResultData.builder().code(ResultCode.USERLOGIN_NOT_EXISTS.getCode())
                    .message(ResultCode.USERLOGIN_NOT_EXISTS.getMessage()).build();
        }
        List<Long> ids = positionCollectionDao.selectPositionCollectionByUserId(userId);
        List<PositionList> positionLists = null;
        if (ids.size() != 0) {
            positionLists = positionListDao.selectPositionListByIds(ids);
        }
        return ResultData.success(positionLists);
    }

    @Override
    public ResultData deletePositionCollectionById(PositionCollection positionCollection) {
        boolean flag = false;
        List<Long> list = positionCollectionDao.selectPositionCollectionByUserId(positionCollection.getUserId());
        for (Long l : list) {
            if (l == positionCollection.getPositionId()) {
                flag = true;
            }
        }
        if (!flag) {
            return ResultData.failed();
        } else {
            Long along = positionCollectionDao.deletePositionCollectionById(positionCollection);
            if (along > 0) {
                return ResultData.success("取消收藏成功");
            }
            return ResultData.failed();
        }
    }


}
